﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TSQP02 : ViewBase
    {
        #region ==QPLAN===========
        public static readonly DependencyProperty QPLANProperty = DependencyProperty.Register("QPLAN",
            typeof(string), typeof(TSQP02),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPLANChanged));
        public string QPLAN
        {
            get
            {
                return (string)GetValue(QPLANProperty);
            }
            set
            {
                SetValue(QPLANProperty, value);
                ((VQPPLAN)MV).QPLAN = value;
                Locate();
            }
        }
        private static void OnQPLANChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSQP02;
            if (src != null)
            {
                src.QPLAN = (string)e.NewValue;
            }
        }
        #endregion

        #region ==QPKEY===========
        public static readonly DependencyProperty QPKEYProperty = DependencyProperty.Register("QPKEY",
            typeof(string), typeof(TSQP02),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPKEYChanged));
        public string QPKEY
        {
            get
            {
                return (string)GetValue(QPKEYProperty);
            }
            set
            {
                SetValue(QPKEYProperty, value);
             }
        }
        private static void OnQPKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSQP02;
            if (src != null)
            {
                src.QPKEY = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwGP===========
        public static readonly DependencyProperty dwGPProperty = DependencyProperty.Register("dwGP",
            typeof(DataRow), typeof(TSQP02),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwGPChanged));
        public DataRow dwGP
        {
            get
            {
                return (DataRow)GetValue(dwGPProperty);
            }
            set
            {
                SetValue(dwGPProperty, value); 
            }
        }
        private static void OdwGPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSQP02;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwGP = row;
            }
        }
        #endregion


        public TSQP02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            MV = new VQPPLAN();
            this.DataContext = MV; 
            InitializeComponent();
         }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwGP = dwMaster;
            if (dwGP != null)
            {
                QPKEY = dwGP["QPPPNRKRS"].ToString() + dwGP["QPPPLFDNR"].ToString();
            }
            else
            {
                QPKEY = "";
            }
        }
      
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
            Load();
        } 

        public void Load()
        {
            if (vm == null)
                return;
            dwData = TBMain.NewRow();
            dwData["QPPMANDANT"] = app.Mandant;
            dwData["QPPARTST"] = ARTST;
            dwData["QPPPNRKRS"] = KRS; 
            SResult = ((VQPPLAN)MV).QueryQPPLAN(dwData, -1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["DES"] = Helpers.Helper.GetGB2312Value(row["QPPSPEZIF"].ToString());
            }
            MergeData(SResult,true);
            vwMaster.View.MoveCurrentToFirst();
        }

        public override void OnKRS()
        {
            base.OnKRS(); 
            Load();
        }
        public void Locate()
        {
            if (TBMain == null)
                return;
            if (TBMain.Rows.Count == 0)
                return;
            int idx = -1; 
            var query = from DataRow row in TBMain.AsEnumerable()
                        where row["QPPPNRKRS"].ToString() + row["QPPPLFDNR"].ToString() ==QPLAN
                        select row;
            foreach (DataRow row in query)
            {
                 idx = TBMain.Rows.IndexOf(row);              
                break;
            }
            vwMaster.View.MoveCurrentTo(idx);
        }

        public override void ManageCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                Load();
            }
            else
            {
                VICC_WFEVENTS.SVWF.QP02(this.RUNID);
            }
            base.ManageCommand(sender, e);
        }
    }
}
